package cn.zhz.controller;


import cn.zhz.pojo.Provider;
import cn.zhz.pojo.User;

import cn.zhz.service.provaider.ProviderService;
import cn.zhz.service.provaider.ProviderServiceImpl;
import cn.zhz.tools.Constants;
import cn.zhz.tools.PageSupport;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/provider")
public class ProviderController {
    private static Logger logger = Logger.getLogger(ProviderController.class);
    @Resource
    private ProviderService providerService;

    @RequestMapping(value = "providerlist")
    public String proList(Model model, @RequestParam(value = "queryProCode", required = false) String queryProCode,
                          @RequestParam(value = "queryProName", required = false) String queryProviderName,
                          @RequestParam(value = "pageIndex", required = false) String pageIndex) throws Exception {
        List<Provider> providerList = null;
        //设置页面容量
        int pageSize = Constants.pageSize;
        int currentPageNo = 1;
        if (queryProCode == null) {

            queryProCode = "";
        }
        if (queryProviderName == null) {
            queryProviderName = "";
        }
        if (pageIndex != null) {
            currentPageNo = Integer.valueOf(pageIndex);
        }
        int totalCount = providerService.proCount(queryProCode, queryProviderName);
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        providerList = providerService.getProviderList(queryProCode, queryProviderName, currentPageNo, pageSize);
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProviderName", queryProviderName);
        model.addAttribute("queryProCode", queryProCode);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("param", pages);
        return "providerlist";
    }

    @RequestMapping(value = "/toAddPager")
    public String toAddPager() {
        return "provideradd";
    }

    @RequestMapping(value = "/doAdd")
    public String addProvider(Provider provider, HttpSession session) {
//      provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
        if (providerService.addProvider(provider)) {
            return "redirect:/provider/providerlist";
        }
        return "provideradd";

    }

    @RequestMapping(value = "/view/{id}")
    public String showDetail(@PathVariable int id, HttpServletRequest request) {
        request.setAttribute("provider", providerService.viewProvider(id));

        return "providerview";
    }

    @RequestMapping(value = "/update/{id}")
    public String toModify(@PathVariable Integer id, HttpServletRequest request) {
        request.setAttribute("provider", providerService.viewProvider(id));

        return "providermodify";
    }

    @RequestMapping(value = "/updateProvider", method = RequestMethod.POST)
    public String doModify(Provider provider, HttpServletRequest request, HttpSession session) {

        provider.setModifyBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());

        if (providerService.updateProivder(provider)) {
            return "redirect:/provider/providerlist";
        }

        request.setAttribute("error", "修改失败！");

        return "redirect:/provider/providerlist";
    }

    //异步验证后台删除用户
    @RequestMapping(value="/delProvider",method=RequestMethod.GET)
    @ResponseBody
    public Object delProvider(@RequestParam int proid) {
        HashMap<String,Object> map = new HashMap<String, Object>();
        if (providerService.delProivder(proid)>0){
            map.put("delResult","true");
        }else {
            map.put("delResult","false");
        }
        return JSONArray.toJSONString(map);
    }

    /**
     * 异步获取所有供应商
     * @return
     */
    @RequestMapping(value="/getAllProvider",method=RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ResponseBody
    public Object  getAllProvider() {
        HashMap<String,Object>map=new HashMap<String, Object>();
        List<Provider> providers=providerService.getAllProvider();
        map.put("data",providers);
        return JSONArray.toJSONString(map);
    }

//    @RequestMapping(value = "/list1")
//    public String list(Model model){
//        List<Provider> list=providerService.list();
//        model.addAttribute("providerList",list);
//        return "providerlist";
//    }

}